package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Node;

import android.text.TextUtils;

import com.aplink.generic.constant.type.ListViewSelection;
import com.aplink.generic.pojo.controlspec.ListviewSpec;
import com.aplink.generic.xmlparser.XMLPaserUtil;

public class ListViewFormat {
    public static final String CONTENTDATA = "contentdata";
    public static final String DISPLAYPROP = "displayprop";
    public static final String FOOTER = "footer";
    public static final String HEADER = "header";
    public static final String LINKING_VALUE_PROP = "linkingValueProp";
    public static final String LOAD_ALL = "loadAll";
    public static final String ROWITEM = "rowitem";
    public static final String RPTRDESIGN = "rptrdesign";
    public static final String SELECTION_MODE = "selectionMode";

    public static ListviewSpec parse(final Node node) {
        final ListviewSpec listviewSpec = new ListviewSpec();
        if (node.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(node,
                    ListViewFormat.ROWITEM);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setRowItem(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ListViewFormat.RPTRDESIGN);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setRptrdesign(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ListViewFormat.HEADER);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setHeader(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ListViewFormat.FOOTER);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setFooter(value);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    ListViewFormat.SELECTION_MODE);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setListViewSelection(ListViewSelection.get(value));
            }

            value = XMLPaserUtil.getAttrValue(node, ListViewFormat.LOAD_ALL);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("Y")) {
                    listviewSpec.setLoadAll(true);
                }
            }

            value = XMLPaserUtil.getAttrValue(node,
                    ListViewFormat.LINKING_VALUE_PROP);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setLinkingValueProp(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ListViewFormat.DISPLAYPROP);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setDisplayProperty(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ListViewFormat.CONTENTDATA);
            if (!TextUtils.isEmpty(value)) {
                listviewSpec.setContentDatal(value);
            }
        }
        return listviewSpec;
    }
}
